Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

滲透測試重新打底(3.9)--論Web情蒐工具Shodan

滲透測試重新打底(3.9)--論Web情蒐工具Shodan

Week3:hw2 水仙花數

Week3:hw2 水仙花數

Vue.js 學習旅程Mile 9 – Event Handling 事件處理篇-1:methods & v-on

Vue.js 學習旅程Mile 9 – Event Handling 事件處理篇-1:methods & v-on






留言討論